<?php

namespace polymerize\user\module\user;

use app\common\InstanceTrait;
use app\common\SmsType;
use app\model\User as UserModel;
use support\Cache;

class RelieveMobile
{
    use InstanceTrait;

    /**
     * 解除手机号
     *
     * @param int    $uid
     * @param string $mobile
     * @param int    $smsCode
     * @return array
     */
    public function relieveMobile(int $uid, string $mobile, int $smsCode): array
    {
        $sms_key = 'SMS_CODE_' . md5($mobile . SmsType::SMS_CODE_RELIEVE_MOBILE->value);
        if ($smsCode != Cache::get($sms_key)) return ['code' => 400, 'msg' => '短信验证码错误'];

        $where[]  = ['id', '=', $uid];
        $where[]  = ['mobile', '=', $mobile];
        $userInfo = UserModel::getOne($where, '*', 3600);
        if (!$userInfo) return ['code' => 400, 'msg' => '该用户手机号不存在'];

        $update = [
            'mobile' => '',
            'u_time' => myDate(),
        ];
        $res    = UserModel::update($update, $where);

        return $res ? ['code' => 200, 'msg' => ''] : ['code' => 400, 'msg' => '解除失败'];
    }
}
